﻿using PhotoGallery.Data.Entity;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Xml.Linq;

namespace PhotoGallery.Data.Repository
{
    public class VideoAttachmentsRepository : BaseRepository
    {
        private List<VideoAttachments> videos;
        private XDocument doc;
        string path;
        Album album;

        public VideoAttachmentsRepository(int albumID, int videoID, string owner)
        {
            Owner = owner;
            LoadData(albumID, videoID);
        }

        public VideoAttachmentsRepository(int albumID, int videoID, string owner, string albumFolder)
        {
            Owner = owner;
            LoadData(albumID, videoID, albumFolder);
        }

        public IEnumerable<VideoAttachments> Get()
        {
            return videos;
        }

        public VideoAttachments Get(int id)
        {
            return videos.FirstOrDefault(m => m.ID == id);
        }

        public VideoAttachments GetFirst()
        {
            return videos.FirstOrDefault();
        }

        public int Create(VideoAttachments video)
        {
            video.ID = GetMaxValue() + 1;
           
            videos.Add(video);
            doc.Root.Add(new XElement("video",
                              new XElement("id", video.ID),
                              new XElement("filename", video.FileName),
                              new XElement("videoID", video.VideoID)
                              ));

            doc.Save(path + Settings.VideoAttachmentsMetaDataXml);
            return video.ID;
        }

        public void Update(VideoAttachments video)
        {
            
            XElement node = doc.Root.Elements("video").Where(i => (int)i.Element("id") == video.ID).FirstOrDefault();

            node.SetElementValue("id", video.ID);
            node.SetElementValue("filename", video.FileName);
            node.SetElementValue("videoID", video.VideoID);

            doc.Save(path + Settings.VideoAttachmentsMetaDataXml);
        }

        public void Delete(int id)
        {
            VideoAttachments video = videos.FirstOrDefault(i => i.ID == id);

            if (video.FileName != string.Empty && System.IO.File.Exists(path + video.FileName))
            {
                System.IO.File.Delete(path + video.FileName);
            }
            
            doc.Root.Elements("video").Where(i => (int)i.Element("id") == id).Remove();
            doc.Save(path + Settings.VideoAttachmentsMetaDataXml);
        }

        #region privateMethods

        private int GetMaxValue()
        {
            int count = doc.Descendants("video").Count();
            if (count > 0)
            {
                var maxID = (from prod in doc.Descendants("video")
                             select
                             (
                                         int.Parse(prod.Element("id").Value)
                             )).Max();

                return maxID;
            }
            else
            {
                return 0;
            }
        }

        private void LoadData(int albumID, int videoID, string albumFolder = "")
        {
            videos = new List<VideoAttachments>();
            string albumUrl = "/Videos/";
            if (String.IsNullOrEmpty(albumFolder))
            {
                album = new AlbumRepository(Owner).Get(albumID);
                path = BasePath + Settings.UploadsFolder + Owner + "\\Videos\\" + getAlbumFolder(album);
                albumUrl += getAlbumUrl(album);
            }
            else
            {
                path = BasePath + Settings.UploadsFolder + Owner + "\\Videos\\" + albumFolder + "\\";
                albumUrl += albumFolder + "/";
            }

            doc = XDocument.Load(path + Settings.VideoAttachmentsMetaDataXml);
            var qry = from item in doc.Descendants("video")
                      select new VideoAttachments(
                                         Convert.ToInt32(item.Element("id").Value),
                                         item.Element("filename").Value,
                                         Convert.ToInt32(item.Element("videoID").Value)
                                       );
            videos.AddRange(qry.Where(x => x.VideoID == videoID).OrderByDescending(x => x.ID).ToList<VideoAttachments>());
        }


        #endregion
    }
}